/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.order.service.impl;

import iaas.enums.StatusEnum;
import iaas.service.impl.BaseServiceImpl;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wealth.coupon.util.LabelFileEncrypt;
import com.wealth.order.entity.OrderVirtual;
import com.wealth.order.repository.OrderVirtualDao;
import com.wealth.order.repository.VoucherRecordDao;
import com.wealth.order.service.IOrderVirtualService;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class OrderVirtualServiceImpl extends BaseServiceImpl<OrderVirtual> implements IOrderVirtualService{

	@Autowired
	private OrderVirtualDao orderVirtualDao;
	@Autowired
	private VoucherRecordDao voucherRecordDao;



	@Override
	public OrderVirtual createOrderVirtual() {
		OrderVirtual orderVirtual=new OrderVirtual();
		orderVirtual.setCreateTime(new Date());
		orderVirtual.setUid(generateUid());
		orderVirtual.setStatus(StatusEnum.NORMAL.getValue());
		orderVirtual.setStartTime(new Date());
		orderVirtual.setEndTime(getNextDay(new Date()));
		orderVirtualDao.save(orderVirtual);
		return orderVirtual;
	}
	
	public static Date getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +7);//+1今天的时间加一天
		date = calendar.getTime();
		return date;
	}
	
	/**
	 * 产生四位唯一标识 32机制的字符串
	 */
	private String generateUid() {
		String uid = RandomStringUtils.random(4, LabelFileEncrypt.BASE32);
		OrderVirtual orderVirtual = orderVirtualDao.findByUid(uid);
		if(orderVirtual==null){
			return uid;
		}else{
			return generateUid();
		}
	}
	
	
}
